package com.housediyadmin.view.components
{
	/**
	 * 
	 * @author wenjun cai
	 * 2010/11/24
	 */	
	import com.housediyadmin.events.buildingEvents.AddBuildingEvent;
	import com.housediyadmin.events.buildingEvents.EditBuildingEvent;
	import com.housediyadmin.model.AreaTreeModel;
	import com.housediyadmin.model.BuildingModel;
	import com.housediyadmin.vo.Building;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class EditBuildingMediator extends Mediator
	{
		[Inject]
		public var view:EditBuildingView;
		[Inject]
		public var buildingTreeModel:AreaTreeModel;
		[Inject]
		public var buildingModel:BuildingModel;
		
		override public function onRegister():void
		{
			var dateFormatter:DateFormatter = new DateFormatter();    
			dateFormatter.formatString = "YYYY年MM月DD日";   
			
			if(view.type=="ADD")
			{
				view.areaName.text=buildingTreeModel.selectedNode.@typeName;
				view.buildingCreatorTime.text= dateFormatter.format(new Date());
				view.buildingEditorTime.text= dateFormatter.format(new Date());
			}
			else if(view.type=="EDIT")
			{
				BindingUtils.bindProperty(view.areaName, "text", buildingTreeModel.selectedNode, "@typeName");
				BindingUtils.bindProperty(view.buildingName, "text", buildingModel.selectedBuilding, "buildingName");
				BindingUtils.bindProperty(view.buildingAddress, "text", buildingModel.selectedBuilding, "buildingAddress");
				BindingUtils.bindProperty(view.buildingDeveloper, "text", buildingModel.selectedBuilding, "buildingDeveloper");
				BindingUtils.bindProperty(view.buildingWebsite, "text", buildingModel.selectedBuilding, "buildingWebsite");
				BindingUtils.bindProperty(view.buildingTel, "text", buildingModel.selectedBuilding, "buildingTel");
				BindingUtils.bindProperty(view.buildingCreator, "text", buildingModel.selectedBuilding, "buildingCreator");
				BindingUtils.bindProperty(view.buildingEditor, "text", buildingModel.selectedBuilding, "buildingEditor");
				
				var date:Date=buildingModel.selectedBuilding.buildingCreateDate;
				view.buildingCreatorTime.text=dateFormatter.format(date);
				view.buildingEditorTime.text= dateFormatter.format(new Date());
			}
			
			view.okBtn.addEventListener(MouseEvent.CLICK,okClk);
			view.cancelBtn.addEventListener(MouseEvent.CLICK,close);
			view.addEventListener(CloseEvent.CLOSE,close);
		}
		
		private function okClk(e:MouseEvent):void
		{
			
			var i:int = 0;
			var xml:XMLList = buildingTreeModel.selectedNode..level;
			var areaIds:Array = new Array();
			areaIds.push((int)(buildingTreeModel.selectedNode.@typeID));
			for(i; i < xml.length(); i++)
			{
				areaIds.push((int)(xml[i].@typeID));	
			}
			
			
			if(view.type=="ADD")
			{
				var building:Building=new Building();
				
				building.buildingName=view.buildingName.text;
				building.buildingAddress=view.buildingAddress.text;
				building.buildingDeveloper=view.buildingDeveloper.text;
				building.buildingWebsite=view.buildingWebsite.text;
				building.buildingTel=view.buildingTel.text;
				building.buildingCreator=view.buildingCreator.text;
				building.buildingEditor=view.buildingEditor.text;
				building.areaID=buildingTreeModel.selectedNode.@typeID;
				
				building.buildingCreateDate=new Date();
				building.buildingEditDate=new Date();
				
				
				dispatch(new AddBuildingEvent(AddBuildingEvent.ADD_BUILDING,building, areaIds));
				Alert.show("添加成功！");
				
			}
			else if(view.type=="EDIT")
			{
				buildingModel.selectedBuilding.buildingName=view.buildingName.text;
				buildingModel.selectedBuilding.buildingAddress=view.buildingAddress.text;
				buildingModel.selectedBuilding.buildingDeveloper=view.buildingDeveloper.text;
				buildingModel.selectedBuilding.buildingWebsite=view.buildingWebsite.text;
				buildingModel.selectedBuilding.buildingTel=view.buildingTel.text;
				buildingModel.selectedBuilding.buildingCreator=view.buildingCreator.text;
				buildingModel.selectedBuilding.buildingEditor=view.buildingEditor.text;
				buildingModel.selectedBuilding.areaID=buildingTreeModel.selectedNode.@typeID;
				buildingModel.selectedBuilding.buildingCreateDate=buildingModel.selectedBuilding.buildingCreateDate;
				buildingModel.selectedBuilding.buildingEditDate=new Date();
				
				dispatch(new EditBuildingEvent(EditBuildingEvent.EDIT_BUILDING,buildingModel.selectedBuilding, areaIds));
				Alert.show("修改成功！");
			}
			close(e);
		}
		
		private function close(e:Event):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator(this);
		}
	}
}